package org.afun.csms_android;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import org.afun.csms_android.domain.User;
import org.afun.csms_android.utils.MyTableOperator;
import org.afun.csms_android.utils.TableOperator;

public class UserAddActivity extends AppCompatActivity {
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_add);
        mContext=this;

        EditText userAccountEdit=findViewById(R.id.userAccount);
        EditText pwdEdit=findViewById(R.id.userPassword);
        EditText userNameEdit=findViewById(R.id.userName);
        RadioGroup roleGroup=findViewById(R.id.roleGroup);
        RadioGroup genderGroup=findViewById(R.id.genderGroup);
        Button ok=findViewById(R.id.addUserOk);
        Button cancel=findViewById(R.id.cancel);

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account=userAccountEdit.getText().toString();
                String password=pwdEdit.getText().toString();
                String username=userNameEdit.getText().toString();
                RadioButton radioSelectRole=findViewById(roleGroup.getCheckedRadioButtonId());
                RadioButton radioSelectGender=findViewById(genderGroup.getCheckedRadioButtonId());
                String gender=radioSelectGender.getText().toString();
                int roleId=1;
                if(radioSelectRole.getText().toString().equals("管理员")){
                    roleId=1;
                }else if(radioSelectRole.getText().toString().equals("教师")){
                    roleId=2;
                }else if(radioSelectRole.getText().toString().equals("学生")){
                    roleId=3;
                }

                User user = new User();
                user.setAccount(account);
                user.setPassword(password);
                user.setName(username);
                user.setRoleId(roleId);
                user.setGender(gender);

                MyTableOperator myTableOperator=new MyTableOperator(mContext);
                TableOperator tableOperator = myTableOperator.getTableOperator();
                boolean b = tableOperator.addUser(user);

                if(b){
                    new AlertDialog.Builder(mContext)
                            .setTitle("提示信息")
                            .setMessage("添加成功！")
                            .setPositiveButton("确定",null)
                            .create()
                            .show();
                }else{
                    new AlertDialog.Builder(mContext)
                            .setTitle("提示信息")
                            .setMessage("添加失败...")
                            .setPositiveButton("确定",null)
                            .create()
                            .show();
                }

            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}